// LineCounter2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	ifstream fin;
  string dir, filepath;
  int num;
  DIR *dp;
  struct dirent *dirp;
  struct stat filestat;
  int totalLines = 0, fileCount = 0;
  int runCount = 0;
  char reset;
  do{
  cout << "Directory to get files of (type \"exit\" to close): " ;
  getline( cin, dir );  // gets everything the user ENTERs
  if (runCount > 0 && dir != "exit"){
  cout << "Reset Counts? (f)ile Count, (l)ine count, (b)oth, (n)one:";
	cin >> reset;
	cin.ignore();
	if (reset == 'f' || reset == 'F') {
		fileCount = 0;
	}
	if (reset == 'l' || reset == 'L') {
		totalLines = 0;
	}
	if (reset == 'b' || reset == 'B') {
		fileCount = 0;
		totalLines = 0;
	}
  }
  dp = opendir( dir.c_str() );
  if (dp == NULL)
    {
    cout << "Error(" << errno << ") opening " << dir << endl;
    return errno;
    }

  while ((dirp = readdir( dp )))
    {
    filepath = dir + "/" + dirp->d_name;
	int number_of_lines = 0, track=1;
    string line;
    ifstream myfile(filepath);
    while (myfile.good())
	{
        getline(myfile, line);
            ++number_of_lines;
	}
	if (number_of_lines > 0 && filepath.find(".db") == -1 && filepath.find(".zip") == -1 && filepath.find(".ico") == -1) {
		totalLines += number_of_lines;
		fileCount++;
		cout << "Number of lines for file "<< filepath << " is "<<number_of_lines<< endl; 
	}
	
    }
  cout << "Total Lines:" << totalLines << endl;
  cout << "Total Files:" << fileCount << endl;
  closedir( dp );
  runCount++;
  }
  while (dir != "EXIT");
	system("pause"); 
	return 0;
}

